package com.qf.first.controller;

import com.qf.first.entity.Order;
import com.qf.first.exceptions.BaseException;
import com.qf.first.pojo.vo.OrderVo;
import com.qf.first.pojo.vo.RespVo;
import com.qf.first.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api("订单模块")
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController{
    @Autowired
    private OrderService orderService;

    @ApiOperation("根据用户id获取订单")
    @GetMapping("/selectOrderByUserId")
    public RespVo selectOrderByUserId(HttpSession session, Order order){
        Long id =(Long) session.getAttribute("id");
        if (id==null){
            throw new BaseException(RespVo.CodeType.FAIL.getValue(),"用户未登录");
        }
        order.setUserId(id);
        List<OrderVo> orderVo = orderService.selectOrderByUserId(order);
        System.out.println(orderVo);
        if (orderVo.size()==0){
            return convertFail("你的订单空空如也");
        }
        return  convertSuccess(orderVo);
    }
    @ApiOperation("根据商户id获取订单")
    @GetMapping("/selectOrderByShop")
    public RespVo selectOrderByShop(HttpSession session, Order order){
        Long id =(Long) session.getAttribute("shopId");
        if (id==null){
            throw new BaseException(RespVo.CodeType.FAIL.getValue(),"商户未注册");
        }
        order.setShopId(id);
        List<OrderVo> orderVo = orderService.selectOrderByUserId(order);
        System.out.println(orderVo);
        if (orderVo.size()==0){
            return convertFail("你的订单空空如也");
        }
        return  convertSuccess(orderVo);
    }
    @ApiOperation("修改订单状态")
    @GetMapping("/updateOrderState")
    public RespVo updateOrderState(Order order){
        if (order==null){
            throw new BaseException(RespVo.CodeType.FAIL.getValue(),"修改订单状态异常");
        }
        int i = orderService.updateOrder(order);
        if (i==0){
            return convertFail("修改订单状态失败");
        }
        return convertSuccess("修改成功");
    }
}
